<template>
  <div id="questionView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="id" label="题目id">
        <a-input
          v-model="searchParams.id"
          placeholder="请输入题目编号"
          style="min-width: 200px"
        />
      </a-form-item>
      <a-form-item field="title" label="题目名称">
        <a-input v-model="searchParams.title" placeholder="请输入题目名称" />
      </a-form-item>
      <a-form-item field="tags" label="题目标签" style="min-width: 300px">
        <a-input-tag v-model="searchParams.tags" placeholder="请输入题目标签" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSearch">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-table
      :columns="columns"
      :data="data"
      :pagination="{
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
        showTotal: true,
      }"
      @page-change="onPageChange"
      style="margin-top: 20px"
    >
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="toQuestionPage(record)"
            >做题
          </a-button>
        </a-space>
      </template>
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag
            v-for="(tagsItem, index) of record.tags"
            :key="index"
            color="green"
            >{{ tagsItem }}
          </a-tag>
        </a-space>
      </template>
      <template #acceptedRate="{ record }">
        {{
          `${record.submitNum ? record.acceptNum / record.submitNum : 0}%(${
            record.acceptNum
          }/${record.submitNum})`
        }}
      </template>

      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  QuestionControllerService,
  QuestionQueryRequest,
  QuestionVO,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import moment from "moment";
import { useRouter } from "vue-router";

const router = useRouter();
const columns = [
  {
    title: "题号",
    dataIndex: "id",
  },
  {
    title: "题目名",
    dataIndex: "title",
  },
  {
    title: "标签",
    dataIndex: "tags",
    slotName: "tags",
  },
  {
    title: "通过率",
    slotName: "acceptedRate",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    slotName: "createTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];
const data = ref([]);
const total = ref(0);
const searchParams = ref<QuestionQueryRequest>({
  pageSize: 10,
  current: 1,
  title: "",
  tags: [],
  id: undefined,
});
const loadData = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost(
    searchParams.value as any
  );
  if (res.code === 0) {
    data.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("数据加载失败" + res.message);
  }
};
onMounted(() => {
  loadData();
});

//监听loadData函数中的变量值是否发生改变，如果发生改变就从新执行该函数
watchEffect(() => {
  loadData();
});

const toQuestionPage = (question: QuestionVO) => {
  router.push(`/view/question/${question.id}`);
};

const doSearch = () => {
  //重置分页导航栏的当前页为1
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  //console.log("当前搜索条件", searchParams.value);
};
const onPageChange = (changedPageNum: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: changedPageNum,
  };
};
</script>

<style scoped>
#questionView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
